/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007-2008 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.projects.toolbox_basics_project.spring.manager.impl;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.genia.toolbox.persistence.exception.PersistenceException;
import com.genia.toolbox.projects.toolbox_basics_project.bean.InitializationParameterHandler;
import com.genia.toolbox.projects.toolbox_basics_project.bean.PersistableApplicationParameter;
import com.genia.toolbox.projects.toolbox_basics_project.bean.model.Parameter;
import com.genia.toolbox.projects.toolbox_basics_project.business.dao.ByNameReferenceableDAO;
import com.genia.toolbox.projects.toolbox_basics_project.spring.manager.ParameterHandlerManager;
import com.genia.toolbox.projects.toolbox_basics_project.spring.manager.ParameterManager;
import com.genia.toolbox.spring.provider.init.bean.SpringInitializable;

/**
 * implementation of {@link ParameterManager}.
 */
public class ParameterManagerImpl
    implements ParameterManager, SpringInitializable
{

  /**
   * the {@link ByNameReferenceableDAO} to use.
   */
  private ByNameReferenceableDAO byNameReferenceableDAO;

  /**
   * all the declared {@link PersistableApplicationParameter}.
   */
  private Map<String, PersistableApplicationParameter<?>> applicationParameters = new HashMap<String, PersistableApplicationParameter<?>>();

  /**
   * the {@link ParameterHandlerManager} for
   * {@link InitializationParameterHandler}.
   */
  private ParameterHandlerManager initializationParameterHandlerManager;



  /**
   * delete a {@link Parameter}.
   * 
   * @param parameter
   *          the {@link Parameter} to delete
   * @throws PersistenceException
   *           when an error occured
   * @see com.genia.toolbox.projects.toolbox_basics_project.spring.manager.ParameterManager#delete(com.genia.toolbox.projects.toolbox_basics_project.bean.model.Parameter)
   */
  public void delete(Parameter parameter)
      throws PersistenceException
  {
    getByNameReferenceableDAO().delete(parameter);
  }



  /**
   * getter for the applicationParameters property.
   * 
   * @return the applicationParameters
   */
  public Collection<PersistableApplicationParameter<?>> getApplicationParameters()
  {
    return applicationParameters.values();
  }



  /**
   * getter for the byNameReferenceableDAO property.
   * 
   * @return the byNameReferenceableDAO
   */
  public ByNameReferenceableDAO getByNameReferenceableDAO()
  {
    return byNameReferenceableDAO;
  }



  /**
   * getter for the order property. This property allow to sort the
   * {@link SpringInitializable}.
   * 
   * @return the order
   */
  public int getOrder()
  {
    return -100;
  }



  /**
   * returns a {@link Parameter} knowing its reference name.
   * 
   * @param referenceName
   *          the reference name of the parameter
   * @return the {@link Parameter} having the given reference name, or
   *         <code>null</code> if no such object exists
   * @throws PersistenceException
   *           when an error occured
   * @see com.genia.toolbox.projects.toolbox_basics_project.spring.manager.ParameterManager#getParameter(java.lang.String)
   */
  public Parameter getParameter(String referenceName)
      throws PersistenceException
  {
    return getByNameReferenceableDAO().getObjectByReferenceName(Parameter.class, referenceName);
  }



  /**
   * The method to be called at the start of the application.
   * 
   * @throws PersistenceException
   *           when an error occured
   */
  @SuppressWarnings("unchecked")
  public void init()
      throws PersistenceException
  {
    for (PersistableApplicationParameter<?> parameter : getApplicationParameters()) {
      ((InitializationParameterHandler<Object>) getInitializationParameterHandlerManager().getHandler(parameter)).initValue((PersistableApplicationParameter<Object>) parameter);
    }
  }



  /**
   * setter for the applicationParameters property.
   * 
   * @param applicationParameters
   *          the applicationParameters to set
   */
  public void setApplicationParametersList(List<PersistableApplicationParameter<?>> applicationParameters)
  {
    for (PersistableApplicationParameter<?> applicationParameter : applicationParameters) {
      this.applicationParameters.put(applicationParameter.getReferenceName(), applicationParameter);
    }
  }



  /**
   * setter for the byNameReferenceableDAO property.
   * 
   * @param byNameReferenceableDAO
   *          the byNameReferenceableDAO to set
   */
  public void setByNameReferenceableDAO(ByNameReferenceableDAO byNameReferenceableDAO)
  {
    this.byNameReferenceableDAO = byNameReferenceableDAO;
  }



  /**
   * getter for the initializationParameterHandlerManager property.
   * 
   * @return the initializationParameterHandlerManager
   */
  public ParameterHandlerManager getInitializationParameterHandlerManager()
  {
    return initializationParameterHandlerManager;
  }



  /**
   * setter for the initializationParameterHandlerManager property.
   * 
   * @param initializationParameterHandlerManager
   *          the initializationParameterHandlerManager to set
   */
  public void setInitializationParameterHandlerManager(ParameterHandlerManager initializationParameterHandlerManager)
  {
    this.initializationParameterHandlerManager = initializationParameterHandlerManager;
  }



  /**
   * save a not yet persistent object.
   * 
   * @param parameter
   *          the object to save
   * @throws PersistenceException
   *           if an error occured
   * @see com.genia.toolbox.projects.toolbox_basics_project.spring.manager.ParameterManager#save(com.genia.toolbox.projects.toolbox_basics_project.bean.model.Parameter)
   */
  public void save(Parameter parameter)
      throws PersistenceException
  {
    getByNameReferenceableDAO().save(parameter);
  }



  /**
   * update a persistent object.
   * 
   * @param parameter
   *          the object to update
   * @throws PersistenceException
   *           if an error occured
   * @see com.genia.toolbox.projects.toolbox_basics_project.spring.manager.ParameterManager#update(com.genia.toolbox.projects.toolbox_basics_project.bean.model.Parameter)
   */
  public void update(Parameter parameter)
      throws PersistenceException
  {
    getByNameReferenceableDAO().update(parameter);
  }



  /**
   * returns an {@link PersistableApplicationParameter} by its referenceName.
   * 
   * @param referenceName
   *          the reference name of the parameter
   * @return an {@link PersistableApplicationParameter} by its referenceName
   * @see com.genia.toolbox.projects.toolbox_basics_project.spring.manager.ParameterManager#getApplicationParameter(java.lang.String)
   */
  public PersistableApplicationParameter<?> getApplicationParameter(String referenceName)
  {
    return applicationParameters.get(referenceName);
  }

}
